package trabalho.doo3.j2me.telas;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import trabalho.doo3.j2me.bean.Filme;
import trabalho.doo3.j2me.bean.Reserva;
import trabalho.doo3.j2me.midlet.Midlet;

/**
 *
 * @author Matheus
 */
public class TelaFilmes extends List implements CommandListener {

    private Midlet mid;
    private Command btVoltar;
    private Command btSalvar;
    private Command btLimpar;
    private Filme f;
    private String login;
    private Reserva r;
    private String URL = "http://localhost:8080/Trabalho_DOO_III_-_Web_Server/servlet";

    public TelaFilmes(String title, Midlet mid, String login) {
        super(title, List.MULTIPLE);
        this.mid = mid;
        this.login = login;
        this.btVoltar = new Command("Voltar", Command.BACK, 0);
        this.btSalvar = new Command("Salvar", Command.ITEM, 1);
        this.btLimpar = new Command("Limpar", Command.ITEM, 2);
        this.addCommand(btVoltar);
        this.addCommand(btLimpar);
        this.addCommand(btSalvar);
        inicializa();
        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        Display display = Display.getDisplay(mid);
        if (c == btLimpar) {
            limpar();
            inicializa();
        } else if (c == btSalvar) {
            try {
                r = new Reserva();
                boolean retornado[] = new boolean[this.size()];
                this.getSelectedFlags(retornado);
                r.open();
                for (int i = 0; i < retornado.length; i++) {
                    if (retornado[i]) {
                        r.setFilme_id(this.getString(i));
                        r.setDeletar("nao");
                        r.insert(r);
                    }
                }
                r.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
            display = Display.getDisplay(mid);
            TelaReservas telaLogin = new TelaReservas("Tela de reservas", mid, login);
            display.setCurrent(telaLogin);
        } else if (c == btVoltar) {
            display = Display.getDisplay(mid);
            TelaReservas telaLogin = new TelaReservas("Tela de reservas", mid, login);
            display.setCurrent(telaLogin);
        }
    }

    private void inicializa() {
        try {
            f = new Filme();
            f.open();
            Vector todos = f.todos();
            for (int i = 0; i < todos.size(); i++) {
                Filme listaBean = (Filme) todos.elementAt(i);
                append(listaBean.getNome(), null);
            }
            f.close();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void limpar() {
        try {
            f.clear();
            deleteAll();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
//    private class ConexaoWWW implements Runnable {
//
//        public void run() {
//            HttpConnection httpConn = null;
//            try {
//                httpConn = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);
//                httpConn.setRequestMethod(HttpConnection.POST);
//                httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//                httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
//                httpConn.setRequestProperty("Content-Language", "pt-BR");
//                httpConn.setRequestProperty("Accept", "application/octet-stream");
//                httpConn.setRequestProperty("Connection", "close");
//                OutputStream os = httpConn.openOutputStream();
//                os.write("acao=Busca Aluno".getBytes());
//                os.close();
//
//                String msg = httpConn.getResponseMessage();
//                int code = httpConn.getResponseCode();
//                if (code == HttpConnection.HTTP_OK) {
//                    DataInputStream dis = httpConn.openDataInputStream();
//                    String leitura = null;
//                    do {
//                        leitura = dis.readUTF();
//                        if (!"#Encerra leitura#".equals(leitura)) {
//                            append(leitura, null);
//                        }
//                    } while (!"#Encerra leitura#".equals(leitura) && leitura != null);
//                    dis.close();
//                } else {
//                    System.out.println("Código invalido: " + msg);
//                }
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
//    }
}
